package com.hwua.view;

import com.hwua.entity.Student;
import com.hwua.entity.User;
import com.hwua.service.StudentService;
import com.hwua.service.UserService;
import com.hwua.service.impl.StudentServiceImpl;
import com.hwua.service.impl.UserServiceImpl;
import com.hwua.tqm.FatherTable;

import java.sql.SQLException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.util.regex.Pattern.compile;

/**
 * @author CQY
 */
public class UserView extends FatherTable{

    @Override
    public String[] columnNamesInit() {
        String[] s={"用户编号","用户名","密码","邮箱","电话","权限"};
        return s;
    }

    @Override
    public Object[][] tableDataInit() {
        UserService service=new UserServiceImpl();
        List<User> list=service.findAll();
        int row=list.size();
        Object[][] date=new Object[row][6];
        for (int i = 0; i <date.length ; i++) {
            User user=list.get(i);
            date[i][0]=user.getAdmin_id();
            date[i][1]=user.getAdmin_name();
            date[i][2]=user.getAdmin_pwd();
            date[i][3]=user.getAdmin_email();
            date[i][4]=user.getAdmin_phone();
            date[i][5]=user.getRole();
        }
        return date;
    }

    @Override
    public void deleteMoreAction(Object[] ids) {

        UserService service=new UserServiceImpl();
        for (int i = 0; i <ids.length ; i++) {
            User user=new User();
            service.delete((int)ids[i]);
        }

    }

    @Override
    public void addSaveAction(Object[] obj) {
       /* 1.完善项目  做管理员添加验证功能
        使用正则表达式进行验证
        1.邮箱必须是QQ邮箱
        2.电话号码必须是 18 15 13 14 17开头
        3.密码必须包括字母和数字
        4.用户名必须是四个字母以上*/

        UserService service=new UserServiceImpl();
        int id=(Integer.parseInt(obj[0].toString())) ;
        String name=(String)obj[1];
        String pwd=(String)obj[2];
        String email=(String)obj[3];
        String phone=(String)obj[4];
        String role=(String)obj[5];
        User user=null;

        //定义正则表达式   数组并且是六位
        Pattern pt=compile("\\d{8,10}(@qq.com)$");
        //验证正则表达式
        Matcher mt=pt.matcher(email);
        //mt.matches()  返回一个boolean 满足条件返回true
        if(!mt.matches()){
            System.out.println("email输入格式不正确，必须是QQ邮箱");
        }else {
            pt=compile("[a-zA-Z]{4,}");
            mt=pt.matcher(name);
            if(!mt.matches()){
                System.out.println("用户名输入错误，必须是三位以上的字母");
            }else {
                pt=compile("\\w{6,}");
                mt=pt.matcher(pwd);
                if(!mt.matches()){
                    System.out.println("密码输入错误，必须是五位以上的数字和字母");
                }else {
                    pt=compile("(18|15|13|14|17)\\d{9}");
                    mt=pt.matcher(phone);
                    if(!mt.matches()){
                        System.out.println("电话号码输入错误");
                    }else {
                         user=new User(id,name,pwd,email,phone,role);
                        int row=service.save(user);
                    }
                }
            }
        }



    }

    @Override
    public void updateSaveAction(Object[] obj) {
        UserService service=new UserServiceImpl();
        int id=(int) obj[0];
        String name=(String)obj[1];
        String pwd=(String)obj[2];
        String email=(String)obj[3];
        String phone=(String)obj[4];
        String role=(String)obj[5];

        Pattern pt=compile("\\d{8,10}(@qq.com)$");
        //验证正则表达式
        Matcher mt=pt.matcher(email);
        //mt.matches()  返回一个boolean 满足条件返回true
        if(!mt.matches()){
            System.out.println("email输入格式不正确，必须是QQ邮箱");
        }else {
            pt=compile("[a-zA-Z]{4,}");
            mt=pt.matcher(name);
            if(!mt.matches()){
                System.out.println("用户名输入错误，必须是三位以上的字母");
            }else {
                pt=compile("\\w{6,}");
                mt=pt.matcher(pwd);
                if(!mt.matches()){
                    System.out.println("密码输入错误，必须是五位以上的数字和字母");
                }else {
                    pt=compile("(18|15|13|14|17)\\d{9}");
                    mt=pt.matcher(phone);
                    if(!mt.matches()){
                        System.out.println("电话号码输入错误");
                    }else {
                        User user=new User(id,name,pwd,email,phone,role);
                        int row=service.update(user);
                    }
                }
            }
        }


    }

    @Override
    public Object[][] searchAction(String keyword) {
        StudentService service=new StudentServiceImpl();
        List<Student> list=service.findSearch(keyword);
        int row=list.size();
        Object[][] date=new Object[row][4];
        for (int i = 0; i <date.length ; i++) {
            Student student=list.get(i);
            date[i][0]=student.getStuId();
            date[i][1]=student.getStuName();
            date[i][2]=student.getStuAge();
            date[1][3]=student.getStuSex();
        }
        return date;
    }

    @Override
    public void deleteAction(Object id) {

        StudentService service=new StudentServiceImpl();
            Student student=new Student();
        try {
            service.delete((String)id);
        } catch (SQLException e) {
            e.printStackTrace();

        }

    }
}
